package com.heima.wemedia.scheduler;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmNewsMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.net.nntp.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class NewsScheduler {

    @Autowired
    private WmNewsMapper wmNewsMapper;

    // 每分钟检查一次待发布的文章
    @Scheduled(fixedRate = 60 * 1000)
    public void checkScheduledNews() {
        LocalDateTime now = LocalDateTime.now();
        List<WmNews> newsList = wmNewsMapper.selectList(Wrappers.<WmNews>lambdaQuery()
                .le(WmNews::getPublishTime, now)
                .eq(WmNews::getStatus, 0)
        );

        newsList.forEach(news -> {
            news.setStatus((short) 1);
            news.setEnable(1);
            wmNewsMapper.updateById(news);
            // 这里可以添加发布后的其他逻辑，如通知、日志记录等
        });
    }
}